// pages/profile/profile.js
import { getUser } from '../../apis/user_api'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    watermarkText: wx.getStorageSync("name") + wx.getStorageSync("phoneNumber").slice(-4),
    phone: '',
    address: "",
    addressList: [],
    avatarUrl: "/images/user.png",
    nickname: '',
    points: 0,
    isAdmin: false
  },
  getUserProfile(e) {
    wx.getUserProfile({
      desc: '获取头像',
      success: (res) => {
        wx.setStorage({
          key: "avatarUrl",
          data: res.userInfo.avatarUrl
        })
        wx.setStorage({
          key: "nickname",
          data: res.userInfo.nickName
        })
        this.setData({
          nickname: res.userInfo.nickName,
          avatarUrl: res.userInfo.avatarUrl
        })
      }
    })


  },
  //修改负责辖区
  getAddress(e) {
    let addressList = this.data.addressList
    let address = addressList[e.detail.value]
    wx.setStorage({
      key: "address",
      data: address
    })
    this.setData({
      address
    })
  },
  //初始化页面数据
  getPageData() {
    let phone = wx.getStorageSync('phoneNumber')
    let nickname = wx.getStorageSync('nickname') ? wx.getStorageSync('nickname') : phone
    let name = wx.getStorageSync('name')
    let address = wx.getStorageSync('address')
    let addressList = wx.getStorageSync('addressList')
    let avatarUrl = wx.getStorageSync('avatarUrl') ? wx.getStorageSync
      ('avatarUrl') : "/images/user.png"
    let adminPhoneList = getApp().globalData.adminPhoneList
    if (phone != "" && phone != undefined) {
      getUser(phone).then(res => {
        if (res.success) {
          wx.setStorage({
            key: "avatarUrl",
            data: "/images/user.png"
          })
          wx.setStorage({
            key: "points",
            data: res.data.points
          })
          this.setData({
            nickname,
            name,
            address,
            addressList,
            avatarUrl,
            points: res.data.points,
            isAdmin: adminPhoneList.indexOf(phone) >= 0
          })
        }
      });
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getPageData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})
